package com.team6.stickynote.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.PopupMenu;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.team6.stickynote.App;
import com.team6.stickynote.R;
import com.team6.stickynote.adapter.StickyNoteAdapter;
import com.team6.stickynote.adapter.StickyNoteDiffCallback;
import com.team6.stickynote.base.BaseActivity;
import com.team6.stickynote.databinding.ActivitySearchBinding;
import com.team6.stickynote.util.ToastUtil;

public class SearchActivity extends BaseActivity {

    private ActivitySearchBinding binding;

    private SearchViewModel searchViewModel;

    private StickyNoteAdapter stickyNoteAdapter;

    @Override
    protected void initBinding() {
        binding = ActivitySearchBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        searchViewModel = new ViewModelProvider(this).get(SearchViewModel.class);

        stickyNoteAdapter = new StickyNoteAdapter((new StickyNoteDiffCallback()));
        stickyNoteAdapter.setOnItemClickListener(id -> {
            Intent intent = new Intent(SearchActivity.this, EditActivity.class);
            intent.putExtra(EditActivity.EXTRA_STICKY_NOTE_BEAN_ID, id);
            startActivity(intent);
        });
        stickyNoteAdapter.setOnItemLongClickListener((view, id) -> {
            //长按事件
            //为该View设置弹出框
            PopupMenu popupMenu = new PopupMenu(SearchActivity.this, view);
            popupMenu.getMenuInflater().inflate(R.menu.delete_menu, popupMenu.getMenu()); // 设置样式
            popupMenu.show(); // 显示
            //设置点击事件
            popupMenu.setOnMenuItemClickListener(item -> {
                switch (item.getItemId()){
                    case R.id.delete_it:
                        // 删除该item
                        ToastUtil.toast("删除成功！");
                        App.database.deleteOneData(id);
                        // 更新显示数据
                        searchViewModel.updateRepo();
                        break;
                }
                return true;
            });
        });

        binding.rvSearch.setAdapter(stickyNoteAdapter);
        binding.rvSearch.setLayoutManager(new LinearLayoutManager(this));

        binding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                searchViewModel.search(binding.etSearch.getText().toString());
            }
        });

        searchViewModel.stickyNoteBeans.observe(this, stickyNoteBeans -> {
            stickyNoteAdapter.submitList(stickyNoteBeans);
        });
    }

}